/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import client.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author tomek
 */
public class AddingContactGui extends JFrame implements ActionListener {
    
    client.AddingContact myAddingContact;
    
    
    
    public AddingContactGui(client.AddingContact a) {
        super("Dodaj kontakt");
        myAddingContact = a;
        initComponents();
    }
    
    public void showGui() {
        pack();
        setVisible(true);
    }
     static final String titledBorderString = "Podaj login";
    //texts for buttons on bottom
    static final String ADD = "Dodaj";
    static final String CANCEL = "Anuluj";
    
    private JPanel mainPanel, bottomPanel, upperPanel;
    private JTextField textField;

    private JButton jButton1;
    private JButton jButton2;    
    
    
    private void initComponents() {
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(UsersListGui.createIcon(UsersListGui.Menu.ADD.imgURL()).getImage());
        
        //creates JPanel for labels and text field
        mainPanel = new JPanel();
        mainPanel.setLayout(new java.awt.GridLayout(1,1,0,10));
        
        upperPanel = new JPanel();
        upperPanel.setBorder(new javax.swing.border.TitledBorder(titledBorderString));
        textField = new JTextField(10);
        upperPanel.add(textField);
        
        bottomPanel = new JPanel();
        bottomPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 2, 1));
        
        jButton1 = new JButton();
        jButton2 = new JButton();
        bottomPanel.add(jButton1);
        bottomPanel.add(jButton2);

        mainPanel.add(upperPanel);
        mainPanel.add(bottomPanel);
        add(mainPanel);
        
        jButton1.setText(CANCEL);
        jButton1.setActionCommand(CANCEL);
        jButton2.setText(ADD);      
        jButton2.setActionCommand(ADD);
        
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
  
        setResizable(false);
        
    }

    
    public void showErrorDialog() {
        JOptionPane.showMessageDialog(new JFrame(), "Podczas proby nawiazania polaczenia wystapil blad.\nSprobuj ponownie.", "Blad polaczenia", JOptionPane.ERROR_MESSAGE);
    }
    
    public void showConfirmationDialogAndDispose() {
        JOptionPane.showMessageDialog(new JFrame(), "Dodawanie nowego kontaktu powiodlo sie.", "Uzytkownik dodany", JOptionPane.INFORMATION_MESSAGE);
        dispose();
    }
            
    
    //ActionListener method
    public void actionPerformed(ActionEvent evt) {
        if (ADD.equals(evt.getActionCommand())) {
            String login = null;
            login = textField.getText();

          //sprawdzanie czy podany login istnieje
            myAddingContact.sendAddRequest(login);

            
        }
        else dispose();
    }

}
